<?php
class Pageley_Query_Basic implements Pageley_Query {
	private $type;
	private $condition;
    private $order;
    private $range;
	private $propertyMap;

	public function __construct($type, $condition = null, $order = null, $range = null) {
		$this->type = $type;
		$this->condition = $condition;
        $this->order = $order;
        $this->range = $range;
		$this->propertyMap = new Pageley_Util_ArrayMap();
		if($condition != null) {
			$condition->setQuery($this);
		}
	}

	public function getNextIndex($propertyName) {
		if(!$this->propertyMap->containsKey($propertyName)) {
			$this->propertyMap->put($propertyName, 0);
		}
		$index = $this->propertyMap->get($propertyName) + 1;
		$this->propertyMap->put($propertyName, $index);
		return $index;
	}

	public function matches($entity) {
		return $entity->getType() == $this->type &&
			($this->condition === null || $this->condition->matches($entity));
	}

	public function getType() {
		return $this->type;
	}

	public function getCondition() {
		return $this->condition;
	}

	public function setCondition($condition) {
		$this->condition = $condition;
	}

	public function getValues() {
		$values = null;
		if($this->condition != null) {
			$values = $this->condition->getValues();
		} else {
			$values = new Pageley_Util_ArrayMap();
		}
		return $values;
	}

	public function __toString() {
		return "query on " . $this->type . " with " . $this->condition
            . ($this->order !== null ? " ordered by " . $this->order : "")
            . ($this->range !== null ? " ranging from " . $this->range->getStart() . " to " . $this->range->getEnd() : "");
	}
    
    public function getOrder() {
        return $this->order;
    }
    
    public function getRange() {
        return $this->range;
    }
}